<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\CustomerDemand;
use app\admin\model\ClothmakingOrders;
use app\admin\model\ClothmakingProduct;
use app\admin\model\ClothmakingCustomer;

/**
 * 应收账款管理
 *
 * @icon fa fa-circle-o
 */
class AccountsReceivable extends Backend
{

    /**
     * AccountsReceivable模型对象
     * @var \app\admin\model\AccountsReceivable
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\AccountsReceivable;
        $this->view->assign("settlementStatusList", $this->model->getSettlementStatusList());
    }
    // public function add(){
    //     return $this->request->param();
    // }

    public function getTableList()
    {
        $id = $this->request->param("order_ids");
        $orderModel = new ClothmakingOrders();
        $customerDemandModel = new CustomerDemand();
        $rows = [];
        
        if (!empty($id)) {
            // 分割order_id，处理多个订单情况
            $orderIdArr = explode(',', $id);
            foreach ($orderIdArr as $orderId) {
                // 获取订单数据
                $orderData = ClothmakingOrders::get($orderId);
                if ($orderData && !empty($orderData['customer_demand_quantity'])) {
                    // 解析JSON字段
                    $demandQuantities = json_decode($orderData['customer_demand_quantity'], true);
                    
                    if (json_last_error() === JSON_ERROR_NONE && is_array($demandQuantities)) {
                        foreach ($demandQuantities as $item) {
                            // 获取客户需求数据
                            $demandData = $customerDemandModel->where('id', $item['demand_id'])->find();
                            
                            if ($demandData) {
                                $rows[] = [
                                    'product_id' => $demandData['product_id'],
                                    'customer_id' => $demandData['customer_id'],
                                    'product_name' => ClothmakingProduct::get($demandData['product_id'])->name,
                                    'customer_name' => ClothmakingCustomer::get($demandData['customer_id'])->customer_name,
                                    'unit_price' => $demandData['unit_price'],
                                    'quantity' => $item['quantity'],
                                    'demand_id' => $item['demand_id'],
                                    'order_id' => $orderId
                                ];
                            }
                        }
                    }
                }
            }
        }

        $result = [
            'code' => 1,
            'msg' => '获取成功',
            'total' => count($rows),
            'data' => $rows
        ];
        
        return json($result);
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
